<?php
/**
 * 广播功能
 * @author zlc
 * @version 1.0
 * @date: 2020/8/20
 * @time: 11:28
 */
namespace App\Http\Controllers\Home;
use App\Events\PrivateMessageEvent;
use App\Http\Controllers\Controller;
use App\Models\Ad;
use Illuminate\Http\Request;
use App\Events\PublicMessageEvent;
use Illuminate\Support\Facades\Redis;

class NewsController extends Controller
{

    /*
     * 列表页加载
     */
    public function index(){

        return view('home.news.index');
    }

    public function echo(){
        $userId = 4;
        return view('home.news.echo')->with('user_id',$userId);
    }

    /*
     * 发送广播信息的接口
     */
    public function pushMessage(){

        //        $key = 'LIST:TEST:R';
//        $names = ['PHP','HTML','CSS','JavaScript','Node','Java','Ruby','Python'];
//        $info = Redis::set($key,json_encode($names));
//        dd($info);
          $message = Ad::first()->toArray();
          $message = json_encode($message);
//        $message = '这个是广播测试信息';
        broadcast(new PublicMessageEvent($message));
    }

    /*
     * 添加发布广播到私有频道
     */

    public function privateMessage(){
        $user = Ad::find(4);
        if (empty($user)) return '无此用户';
        $message = "私有频道";
        broadcast(new PrivateMessageEvent($user, $message));
    }

}